package com.zcn.io.web.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcn.io.common.query.PayDetailStatisticsQuery;
import com.zcn.io.web.api.entity.StudioExpenses;
import com.zcn.io.web.api.query.StudioExpensesQuery;
import com.zcn.io.web.mapper.StudioExpensesMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/4 21:28
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class StudioExpensesService {
    private final StudioExpensesMapper mapper;

    public void list(Page<StudioExpenses> page, StudioExpensesQuery orderMain) {
        LambdaQueryWrapper<StudioExpenses> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(orderMain.getExpendType())){
            wrapper.eq(StudioExpenses::getExpendType, orderMain.getExpendType());
        }
        if (StrUtil.isNotEmpty(orderMain.getMemo())){
            wrapper.like(StudioExpenses::getMemo, orderMain.getMemo());
        }
        if (StrUtil.isNotEmpty(orderMain.getStartTime())){
            wrapper.ge(StudioExpenses::getExpendTime, orderMain.getStartTime());
        }
        if (StrUtil.isNotEmpty(orderMain.getEndTime())){
            wrapper.le(StudioExpenses::getExpendTime, orderMain.getEndTime());
        }
        if (StrUtil.isNotEmpty(orderMain.getType())){
            wrapper.eq(StudioExpenses::getType, orderMain.getType());
        }
        if (StrUtil.isNotEmpty(orderMain.getExpendWay())){
            wrapper.eq(StudioExpenses::getExpendWay, orderMain.getExpendWay());
        }
        wrapper.orderByDesc(StudioExpenses::getExpendTime);
        wrapper.orderByDesc(StudioExpenses::getId);
        mapper.selectPage(page, wrapper);
    }

    public String getTotalAmount(StudioExpensesQuery orderMain) {
        return mapper.getTotalAmount(orderMain);
    }

    public void save(StudioExpenses orderMain) {
        if(StrUtil.isEmpty(orderMain.getExpendWay())){
            orderMain.setExpendWay("微信");
        }
        mapper.insert(orderMain);
    }

    public void updateById(StudioExpenses orderMain) {
        mapper.updateById(orderMain);
    }

    public void deleteById(String id) {
        mapper.deleteById(id);
    }

    public List<StudioExpenses> listAllByQuery(PayDetailStatisticsQuery query) {
        LambdaQueryWrapper<StudioExpenses> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getStartTime())) {
            wrapper.ge(StudioExpenses::getExpendTime, query.getStartTime());
        }
        if (StrUtil.isNotEmpty(query.getEndTime())) {
            wrapper.le(StudioExpenses::getExpendTime, query.getEndTime());
        }
        return mapper.selectList(wrapper);
    }
}
